<?php
/**
 * ProjectName: Grapes.
 * Created by LemonZ.
 * FileName: Common.php
 * Date: 2019/2/14
 * Time: 1:06
 * Description:
 */

namespace app\admin\controller;


use app\common\controller\Base;
use app\common\model\Admin;
use app\common\model\UserLog;
use think\facade\Request;

class Common extends Base
{
    public function initialize(){
        parent::initialize();
        //此控制器不需要模板布局，所以屏蔽掉
        $this->view->engine->layout(false);
    }

    /**
     * 用户登陆页面
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login()
    {
        $shop_name = getSetting('shop_name');
        $this->assign('shop_name',$shop_name);
        if (session('?admin')) {
            $this->success('已经登录成功，跳转中...',redirect_url());
        }
        if(Request::isPost()){
            $adminModel = new Admin();
            $result = $adminModel->toLogin(input('param.'));
            if($result['status']){
                if(Request::isAjax()){
                    $result['data'] = redirect_url();
                    return $result;
                }else{
                    $this->redirect(redirect_url());
                }
            }else{
                return $result;
            }
        }else{
            return $this->fetch('login');
        }
    }

    /**
     * 用户退出
     */
    public function logout()
    {
        //增加退出日志
        if(session('admin.id')){
            $userLogModel = new UserLog();
            $userLogModel->setLog(session('admin.id'),$userLogModel::USER_LOGOUT);
        }
        session('admin', null);
        $this->success('退出成功','/admin');
    }
}